package com.hanxiang.hankol.utils;

import android.app.Application;
import android.os.Bundle;

import com.baselibrary.bean.DictionaryData;
import com.baselibrary.manager.SensorsDataManager;
import com.baselibrary.utils.HKLogUtils;
import com.baselibrary.utils.SpUtils;
import com.meiqia.core.MQManager;
import com.meiqia.core.bean.MQAgent;
import com.meiqia.core.bean.MQMessage;
import com.meiqia.core.callback.OnClientOnlineCallback;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.activity.MQConversationActivity;
import com.meiqia.meiqiasdk.callback.MQActivityLifecycleCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.okhttp.SendRequest;
import com.okhttp.callbacks.GenericsCallback;
import com.okhttp.sample_okhttp.JsonGenericsSerializator;

import java.util.List;

import okhttp3.Call;

/**
 * @author:hanke 2022/7/4
 * desc:
 */
public class AppUtils {

    public static void initSystemDic() {
        SendRequest.getListByType(
                new GenericsCallback<DictionaryData>(new JsonGenericsSerializator()) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(DictionaryData response, int id) {
                        if (response != null) {
                            if (response.isSuccess()) {
                                SpUtils.getInstance().putLoadDictionaryBean(response);
                            }
                        }
                    }
                });
    }

    public static void initMeiQia(Application application) {
        //美洽
        MQConfig.init(application.getApplicationContext(), "a3eadf0b2d83ef04bf3aabecab8cd859", new OnInitCallback() {
            @Override
            public void onSuccess(String clientId) {
            }

            @Override
            public void onFailure(int code, String message) {
                HKLogUtils.e("美洽客服初始化onFailure=======================code:" + code + "======message==========" + message);
            }
        });
        MQManager mManager = MQManager.getInstance(application.getApplicationContext());
        MQConfig.isShowClientAvatar = true;

        MQConfig.setActivityLifecycleCallback(new MQActivityLifecycleCallback() {
            @Override
            public void onActivityCreated(MQConversationActivity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(MQConversationActivity activity) {
                if (mManager != null) {
                    //获取页面开始时间
                    SensorsDataManager.Companion.getInstance().onCreateTime();
                    mManager.setCurrentClientOnline(new OnClientOnlineCallback() {
                        @Override
                        public void onSuccess(MQAgent mqAgent, String s, List<MQMessage> list) {
                            MQManager.getInstance(application.getApplicationContext()).openMeiqiaService();
                        }

                        @Override
                        public void onFailure(int i, String s) {

                        }
                    });
                }
            }

            @Override
            public void onActivityResumed(MQConversationActivity activity) {

            }

            @Override
            public void onActivityPaused(MQConversationActivity activity) {
                //获取页面结束时间
                SensorsDataManager.Companion.getInstance().onDestroyTime();
                if (SensorsDataManager.Companion.getInstance().getBrowseTime() != 0L) {
                    SensorsDataManager.Companion.getInstance().brookPageView("在线客服");
                }
            }

            @Override
            public void onActivityStopped(MQConversationActivity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(MQConversationActivity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(MQConversationActivity activity) {
                //获取页面结束时间
                SensorsDataManager.Companion.getInstance().onDestroyTime();
                if (SensorsDataManager.Companion.getInstance().getBrowseTime() != 0L) {
                    SensorsDataManager.Companion.getInstance().brookPageView("在线客服");
                }
            }
        });
    }

}
